package jie_yue;

import JDBC.jdbc_user;

import javax.swing.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class create_user extends JFrame {
    public create_user()
    {
        setTitle("学生注册");
        setSize(400, 600);
        setLocation(500,200);
        setLayout(null);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        JLabel label2 = new JLabel("姓名：");
        label2.setBounds(50,110,50,25);
        JTextField textField2 = new JTextField(20);
        textField2.setBounds(100,110,200,25);
        JLabel label3 = new JLabel("性别：");
        label3.setBounds(50,150,50,25);
        JTextField textField3 = new JTextField(20);
        textField3.setBounds(100,150,200,25);
        JLabel label4 = new JLabel("年龄：");
        label4.setBounds(50,190,50,25);
        JTextField textField4 = new JTextField(20);
        textField4.setBounds(100,190,200,25);
        JLabel label5 = new JLabel("专业：");
        label5.setBounds(50,230,50,25);
        JTextField textField5 = new JTextField(20);
        textField5.setBounds(100,230,200,25);
        JLabel label6 = new JLabel("年级：");
        label6.setBounds(50,270,50,25);
        JTextField textField6 = new JTextField(20);
        textField6.setBounds(100,270,200,25);

        add(label2);
        add(textField2);
        add(label3);
        add(textField3);
        add(label4);
        add(textField4);
        add(label5);
        add(textField5);
        add(label6);
        add(textField6);

        JButton button = new JButton("添加");
        button.setBounds(70, 350,80,25);
        JButton button1 = new JButton("取消");
        button1.setBounds(230, 350,80,25);
        add(button);
        add(button1);

        button.addActionListener(e-> add(textField2.getText(), textField3.getText(), textField4.getText(), textField5.getText(), textField6.getText()));
        button1.addActionListener(e-> this.dispose());

        setVisible(true);
    }

    //添加
    private void add(String text, String textField2Text, String textField3Text, String textField4Text, String textField5Text) {
        String b= text;
        String c= textField2Text;
        Integer d= Integer.valueOf(textField3Text);
        String e= textField4Text;
        String f= textField5Text;
        Integer a=0;
        Integer g= 1;
        //查找最大学生ID
        try{
            Connection conn = jdbc_user.getConnection();
            PreparedStatement pstm = conn.prepareStatement("select  MAX(STU_ID) AS MAX_ID from STUDENT");
            // 执行插入
            ResultSet rs = pstm.executeQuery();
            if (rs.next()) {
                a = rs.getInt("MAX_ID") + 1; // 新ID = 最大ID + 1
            }
            pstm.close();
            conn.close();
            JOptionPane.showMessageDialog(null, "插入成功！");
        }catch (SQLException ae){
            showError("查找失败"+ae.getMessage());
        }
        //添加学生信息
        try{
            Connection conn = jdbc_user.getConnection();
            PreparedStatement pstm = conn.prepareStatement("insert into STUDENT(STU_ID,STU_NAME,STU_SEX,STU_AGE,STU_PRO,STU_GRADE,STU_INTEGRITY) values(?,?,?,?,?,?,?)");
            //写入
            pstm.setInt(1,a);
            pstm.setString(2,b);
            pstm.setString(3,c);
            pstm.setInt(4,d);
            pstm.setString(5,e);
            pstm.setString(6,f);
            pstm.setInt(7, g);
            // 执行插入
            int rowsAffected = pstm.executeUpdate();
            if (rowsAffected > 0) {
                JOptionPane.showMessageDialog(null, "插入成功！");
            }
            pstm.close();
            conn.close();
        }catch (SQLException ae){
            showError("添加失败"+ae.getMessage());
        }
    }

    //弹窗
    private void showError(String s) {
        JOptionPane.showMessageDialog(this,s,"错误",JOptionPane.ERROR_MESSAGE);
    }
}
